#ifndef MSGD_INET_H
#define MSGD_INET_H 1

#include <config.h>
#include <core.h>


struct msgd_listening_s {
    void               *data;
    void               *next;
    int                 fd;
    struct sockaddr    *sockaddr;
    msgd_uint_t         socklen;    /* size of sockaddr */
    size_t              addr_text_max_len;

    u_char              *addr_text;
    u_char              *port_text;

    u_char              *out_format;
    u_char              *protocol;

    int                 backlog;
    int                 rcvbuf;
    int                 sndbuf;

    msgd_connection_handler_pt handler;
};


struct msgd_connection_s {
   void            *data;
   msgd_event_t    *read;
   msgd_event_t    *write;
   
   int              fd;
  
   msgd_listening_t *listening;

   struct sockaddr  *sockaddr;
   msgd_uint_t       socklen;

   pool_t           *pool;
   
   queue_t          queue;
  
   msgd_uint_t      close:1;
   msgd_uint_t      reusable:1;
}; 



#ifdef __plusplus
extern "C"  {
#endif

//msgd_int_t msgd_open_listening_socket(const char *host,const char *port);
msgd_int_t msgd_open_listening_socket(msgd_listening_t *ls);

msgd_int_t msgd_setnonblocking(int fd);

void msgd_close_connection(msgd_connection_t *c);

void msgd_init_connection(msgd_connection_t *c);

void msgd_close_accepted_connection(msgd_connection_t *c);

//msgd_int_t msgd_connection_error(msgd_connection_t *c, int err, char *text);

msgd_connection_t *msgd_get_connection(int s);

void msgd_free_connection(msgd_connection_t *c);

void msgd_reusable_connection(msgd_connection_t *c, msgd_uint_t reusable);

#ifdef __plusplus
}
#endif

#endif  //MSGD_INET_H
